package com.ruoyi.supplier.service.impl;

import java.util.List;

import com.ruoyi.common.constant.BusinessConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.NumberUtils;
import com.ruoyi.supplier.domain.TbPrice;
import com.ruoyi.supplier.domain.TbPurchaseDetail;
import com.ruoyi.supplier.mapper.TbCheckProductDetailMapper;
import com.ruoyi.supplier.service.ITbPriceService;
import com.ruoyi.supplier.service.ITbPurchaseDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.supplier.domain.TbCheckProductDetail;
import com.ruoyi.supplier.mapper.TbCheckProductMapper;
import com.ruoyi.supplier.domain.TbCheckProduct;
import com.ruoyi.supplier.service.ITbCheckProductService;

/**
 * 验货Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-07-02
 */
@Service
public class TbCheckProductServiceImpl implements ITbCheckProductService 
{
    @Autowired
    private TbCheckProductMapper tbCheckProductMapper;

    @Autowired
    private TbCheckProductDetailMapper tbCheckProductDetailMapper;
    @Autowired
    private ITbPurchaseDetailService tbPurchaseDetailServiceImpl;
    @Autowired
    private ITbPriceService tbPriceServiceImpl;

    /**
     * 查询验货
     * 
     * @param checkIdPk 验货ID
     * @return 验货
     */
    @Override
    public TbCheckProduct selectTbCheckProductById(Long checkIdPk)
    {
        return tbCheckProductMapper.selectTbCheckProductById(checkIdPk);
    }

    /**
     * 查询验货列表
     * 
     * @param tbCheckProduct 验货
     * @return 验货
     */
    @Override
    public List<TbCheckProduct> selectTbCheckProductList(TbCheckProduct tbCheckProduct)
    {
        return tbCheckProductMapper.selectTbCheckProductList(tbCheckProduct);
    }

    /**
     * 新增验货
     * 
     * @param tbCheckProduct 验货
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTbCheckProduct(TbCheckProduct tbCheckProduct)
    {
        tbCheckProduct.setCreateTime(DateUtils.getNowDate());
        tbCheckProduct.setCreateBy(SecurityUtils.getUsername());
        int rows = tbCheckProductMapper.insertTbCheckProduct(tbCheckProduct);
        insertTbCheckProductDetail(tbCheckProduct);

        return rows;
    }

    /**
     * 修改验货
     * 
     * @param tbCheckProduct 验货
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTbCheckProduct(TbCheckProduct tbCheckProduct)
    {
        tbCheckProduct.setUpdateTime(DateUtils.getNowDate());
        tbCheckProduct.setUpdateBy(SecurityUtils.getUsername());
        tbCheckProductMapper.deleteTbCheckProductDetailByCheckIdPk(tbCheckProduct.getCheckIdPk());
        insertTbCheckProductDetail(tbCheckProduct);
        return tbCheckProductMapper.updateTbCheckProduct(tbCheckProduct);
    }

    /**
     * 批量删除验货
     * 
     * @param checkIdPks 需要删除的验货ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbCheckProductByIds(Long[] checkIdPks)
    {

        for (Long id:checkIdPks ) {
            TbCheckProduct tbCheckProduct = tbCheckProductMapper.selectTbCheckProductById(id);
            if(tbCheckProduct.getCheckStatus()>0){
                return  -1;
            }
        }
        return tbCheckProductMapper.deleteTbCheckProductDetailByCheckIdPks(checkIdPks);
    }

    /**
     * 删除验货信息
     * 
     * @param checkIdPk 验货ID
     * @return 结果
     */
    @Override
    public int deleteTbCheckProductById(Long checkIdPk)
    {
        tbCheckProductMapper.deleteTbCheckProductDetailByCheckIdPk(checkIdPk);
        return tbCheckProductMapper.deleteTbCheckProductById(checkIdPk);
    }

    @Override
    public String selectNumber() {
        String number = tbCheckProductMapper.selectNumber();
        return NumberUtils.autoGenericNumber(number, BusinessConstants.YH);
    }

    @Override
    public int checkBarcode(TbCheckProductDetail tbCheckProductDetail) {
        return tbCheckProductDetailMapper.selectCheckBarcode(tbCheckProductDetail);
    }

    /**
     * 新增验货明细信息
     * 
     * @param tbCheckProduct 验货对象
     */
    public void insertTbCheckProductDetail(TbCheckProduct tbCheckProduct)
    {
        List<TbCheckProductDetail> tbCheckProductDetailList = tbCheckProduct.getTbCheckProductDetailList();
        Long checkIdPk = tbCheckProduct.getCheckIdPk();
        if (StringUtils.isNotNull(tbCheckProductDetailList))
        {
            List<TbCheckProductDetail> list = new ArrayList<TbCheckProductDetail>();
            for (TbCheckProductDetail tbCheckProductDetail : tbCheckProductDetailList)
            {
                tbCheckProductDetail.setCheckIdPk(checkIdPk);
                tbCheckProductDetail.setCheckStatus(tbCheckProduct.getCheckStatus());
                tbCheckProductDetail.setCheckType(tbCheckProduct.getCheckType());
                tbCheckProductDetail.setCheckDate(tbCheckProduct.getCheckDate());
                list.add(tbCheckProductDetail);
                Integer checkStatus = tbCheckProductDetail.getCheckStatus();
                Long checkType = tbCheckProductDetail.getCheckType();

                /**
                 * 提交 && 验货入库
                 * 将采购表中的采购数据回填,回填验货数量
                 */
                if(checkStatus==1&&checkType==0){
                    if(tbCheckProductDetail.getPurchaseDetailId()!=null){
                        TbPurchaseDetail tbPurchaseDetail=new TbPurchaseDetail();
                        tbPurchaseDetail.setId(tbCheckProductDetail.getPurchaseDetailId());
                        tbPurchaseDetail.setCheckQty(tbCheckProductDetail.getQty());
                        tbPurchaseDetailServiceImpl.updateTbPurchaseDetail(tbPurchaseDetail);
                    }

                    /**
                     * 更新最后一次的进货单价
                     */
                    TbPrice tbPrice=new TbPrice();
                    tbPrice.setPriceId(tbCheckProductDetail.getPriceId());
                    tbPrice.setPrice(tbCheckProductDetail.getPrice());
                    tbPriceServiceImpl.updateTbPrice(tbPrice);

                }
            }

            if (list.size() > 0)
            {
                tbCheckProductMapper.batchTbCheckProductDetail(list);
            }
        }
    }
}
